<template>
  <div>
    <!-- 修改密码配置对话框 -->
    <el-dialog
      :title="title"
      center
      :visible="open1"
      :modal="open1"
      @close="close()"
      width="600px"
      :close-on-click-modal="false"
    >
      <el-form ref="form1" :model="form1" :rules="rules" label-width="100px">
        <el-row v-if="flag">
          <el-form-item label="原密码" prop="oldPassword">
            <el-input v-model="form1.oldPassword" placeholder="请输入原密码" type="password" />
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="新密码" prop="newPassword">
            <el-input v-model="form1.newPassword" placeholder="请输入新密码" type="password" />
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="确认新密码" prop="confirmNewPassword">
            <el-input v-model="form1.confirmNewPassword" placeholder="请输入新密码" type="password" />
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitResetPwd">确 定</el-button>
        <el-button @click="close">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getUser, resetUserPwd } from "@/api/authority_app/user/userInfo";
import { getRandomSecretKey } from "@/utils/index";
import { encrypt } from "@/utils/DES3";
export default {
  name: "resetPwd",
  components: {},
  props: {
    id: {
      type: String,
      default: "",
    },
    title: {
      type: String,
      default: "",
    },
    open1: {
      type: Boolean,
      default: false,
    },
    flag: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 表单参数
      form1: {
        id: "",
        oldPassword: "",
        newPassword: "",
        confirmNewPassword: "",
      },
      // 表单校验
      rules: {
        oldPassword: [{ required: true, message: "用户原密码不能为空", trigger: "blur" }],
        newPassword: [{ required: true, message: "用户新密码不能为空", trigger: "blur" },
          {max: 20, message: "新密码不能大于20位", trigger: "blur"}],
        confirmNewPassword: [
          {required: true, message: "用户确认新密码不能为空", trigger: "blur"},
          {max: 20, message: "新密码不能大于20位", trigger: "blur"},
        ],
      },
    };
  },
  created() {
    this.form1.id = this.id;
  },
  methods: {
    // 取消按钮
    close() {
      this.$emit("changeTypeInfoOpen", false);
    },

    /** 提交密码 */
    submitResetPwd: function () {
      this.$refs["form1"].validate((valid) => {
        if (valid) {
          let secretKey = getRandomSecretKey();
          console.log("this.form1",this.form1)
          if (this.flag) {
            this.form1.oldPassword = encrypt(secretKey, this.form1.oldPassword);
          }
          this.form1.newPassword = encrypt(secretKey, this.form1.newPassword);
          this.form1.confirmNewPassword = encrypt(secretKey, this.form1.confirmNewPassword);
          this.form1.key = secretKey;
          resetUserPwd(this.form1).then((response) => {
            this.msgSuccess("修改成功");
            this.close();
          });
        }
      });
    },
  },
};
</script>
